package com.example.books;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.books.adapter.BookAdapter;
import com.example.books.data.Book;
import com.example.books.data.Collection;
import com.example.books.data.User;

import org.litepal.LitePal;

import java.util.List;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private Button collectionBtn;
    private TextView greetText;
    private ListView listBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        addBook();
        initView();
    }

    private void addBook() {
        Book book = new Book();
        book.setBookpath("绝望黎明.txt");
        book.setBookAuthor("宁采成");
        book.setBoolkName("绝望黎明");
        book.save();
        book = new Book();
        book.setBookpath("异界之魔武流氓.txt");
        book.setBoolkName("异界之魔武流氓");
        book.save();
    }
    private void initView() {
        final List<Book> allB = LitePal.findAll(Book.class);
        final List<User> allU = LitePal.findAll(User.class);
        collectionBtn = (Button) findViewById(R.id.collectionBtn);
        collectionBtn.setOnClickListener(this);
        greetText = (TextView) findViewById(R.id.greetText);
        listBook = (ListView) findViewById(R.id.listBook);
        listBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HomeActivity.this,ReadActivity.class);
                intent.putExtra("path", allB.get(i).getBookpath());
                startActivity(intent);
            }
        });
        listBook.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Collection collection = new Collection();
                collection.setBookId(allB.get(i).getId());
                SharedPreferences sp = getSharedPreferences("id", MainActivity.MODE_PRIVATE);
                int id = sp.getInt("sid", 0);
                collection.setUserId(id);
                boolean save = collection.save();
                ImageView imgeView = view.findViewById(R.id.imgColletion);
                imgeView.setSelected(true);
                if(save) {
                    Toast.makeText(HomeActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(HomeActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.collectionBtn:
                startActivity(new Intent(HomeActivity.this,CollectionActivity.class));
                break;
        }
    }
}
